<?php

class Like extends Eloquent {

    const TYPE_POST = 1;
    const TYPE_COMMENT = 2;
    const TYPE_USER = 3;
    const TYPE_ARTICLE = 4;
    
    /**
     * Like a post
     * 
     * @param integer $id
     * @param integer $userId
     */
    public static function likePost($id, $userId) {
        $like = new Like();
        $like->like_type = self::TYPE_POST;
        $like->item_id = $id;
        $like->creator_id = $userId;
        
        $post = Post::find($id);
        DB::transaction(function() use($post, $like){
            $like->save();
            $post_extra = $post->postExtra()->first();
			$post_extra->increment('likes_count');
			$post_extra->touch();  //更新时间戳
			$post_extra->save();
        });
        return $post;
    }
    
    /**
     * Like a comment
     *
     * @param integer $id
     * @param integer $userId
     */
    public static function likeComment($id, $userId) {
        $like = new Like();
        $like->like_type = self::TYPE_COMMENT;
        $like->item_id = $id;
        $like->creator_id = $userId;

        $comment = Comment::find($id);
        DB::transaction(function() use($comment, $like){
            $like->save();
            $comment_extra = $comment->commentExtra()->first();
			$comment_extra->increment('likes_count');
			$comment_extra->touch();
			$comment_extra->save();
        });
        return $comment;
    }
    
    /**
     * Like a user
     *
     * @param integer $id
     * @param integer $userId
     */
    public static function likeUser($id, $userId) {
        $like = new Like();
        $like->like_type = self::TYPE_USER;
        $like->item_id = $id;
        $like->creator_id = $userId;

        $user = User::find($id);
        DB::transaction(function() use($user, $like){
            $like->save();
            $user->increment('likes_count');
			$user->touch();
			$user->save();
        });
        return $user;
    }
    
    /**
     * Like a article
     *
     * @param integer $id
     * @param integer $userId
     */
    public static function likeArticle($id, $userId) {
    	$like = new Like();
    	$like->like_type = self::TYPE_ARTICLE;
    	$like->item_id = $id;
    	$like->creator_id = $userId;
    
    	$article = Article::find($id);
    	DB::transaction(function() use($article, $like){
    		$like->save();
    		$article->increment('likes');
    		$article->save();
    	});                        
    	return $article;
    }
    
    public static function likeExists($likeType, $itemId, $userId) {
    	return Like::where('like_type', $likeType)->OfItemId($itemId)->OfCreatorId($userId)->count() > 0;
    }

    public static function liked_by_post_ids($post_ids)
    {
        return Like::whereIn('item_id', $post_ids)->where('like_type', self::TYPE_POST)->OfCreatorId(Auth::user()->id)->get();
    }

    public function scopeOfItemId($query, $item_id)
    {
        return $query->whereItemId($item_id);
    }

    public function scopeOfLikeType($query, $like_type)
    {
        return $query->whereLikeType($like_type);
    }

    public function scopeOfCreatorId($query, $creator_id)
    {
        return $query->whereCreatorId($creator_id);
    }
    
    public static function getCreateLikeUser($item_id){
    	$likes = Like::where('item_id', $item_id)->where('like_type', Like::TYPE_POST)->get();
    	$users = [];
    	foreach($likes as $like){
    		$users[] = $like->getUserName();
    	}
    	return $users;
    }
    
    public function getUserName(){
    	$user = User::find($this->creator_id);
    	$data = [];
    	$data['user_id'] = $user->id;
    	$data['user_name'] = $user->realname;
    	$data['user_avatar'] = $user->avatar;
    	return $data;    	
    }
}